package com.hhxy.api;

import java.util.Objects;

public class Student {

    private String name;
    private char sex;
    private int age;

    public Student() {

    }

    public Student(String name, char sex, int age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    @Override
    public String toString() {//重写父类Object的toString方法
        return "Student{" +
                "name='" + name + '\'' +
                ", sex=" + sex +
                ", age=" + age +
                '}';
    }

    /**
     *用于比较两个对象的信息是否相同
     * @param obj 待比较的对象
     * @return ture：两个对象信息相同，false：不同
     */
/*    @Override
    public boolean equals(Object obj) {//重写父类Object的equals方法
        if(obj instanceof Student){
            Student s = (Student)obj;//只用Student类型的数据才能调用Student类中的成员
            if( this.name.equals(s.name) && this.age == s.age && this.sex == s.sex ){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }*/

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;//判断
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return sex == student.sex &&
                age == student.age &&
                Objects.equals(name, student.name);
    }

}
class Test{
    public static void main(String[] args) {
        Student s1 = new Student("张三",'男',22);
        Student s2 = new Student("张三",'男',22);
        System.out.println(s1.equals(s2));
        System.out.println(s1);
    }

}
